package component

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commConfig"
	"gitee.com/fierce_wolf/go-fox-edge-common/commLogger"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEnv"
	"strings"
	"sync"
)

type localConfig struct {
	Publish      string
	Subscribe    string
	DeviceNumber string
	GatewayEUI   sync.Map
}

func (e *localConfig) Initialize() {
	commConfig.InitializeByFilePath("serverConfig", "serverConfig.json")

	// 读取MQTT参数
	e.readMqttParam()
}

func (e *localConfig) readMqttParam() {
	// 获得系统配置参数
	configValue, err := commConfig.GetConfigParam("serverConfig")
	if err != nil {
		commLogger.Error("获取mqttConfig参数失败：" + err.Error())
		return
	}

	// 取出系统配置参数
	topic := Map.GetMap(configValue, "topic", make(map[string]interface{}))
	gatewayEUI := Map.GetMap(configValue, "gatewayEUI", make(map[string]interface{}))
	deviceNumber := Map.GetString(configValue, "deviceNumber", "")

	e.DeviceNumber = deviceNumber

	for key, val := range gatewayEUI {
		e.GatewayEUI.Store(key, val)
	}

	publish := Map.GetString(topic, "publish", "")
	subscribe := Map.GetString(topic, "subscribe", "")

	e.Publish = strings.ReplaceAll(publish, "{edgeId}", edgeEnv.GetCPUID())
	e.Subscribe = strings.ReplaceAll(subscribe, "{edgeId}", edgeEnv.GetCPUID())
}
